﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CypherBox
{
    public delegate void SaveRemotePath(string path);
    public partial class SettingsDialog : Form
    {
        public SettingsDialog()
        {
            InitializeComponent();

        }
        string SETTINGS_PATH = Application.StartupPath + "\\settings.xml";
        private void FormSettings_Load(object sender, EventArgs e)
        {
            try
            {
                m_Settings = XMLSerializing.Deserialize<Settings>(SETTINGS_PATH);
                foreach (Job j in m_Settings.jobs)
                {
                    AddListViewItem(j.email, j.token, j.localPath, j.remotePath, j.key);
                }
            }
            catch (FileNotFoundException)
            {
                m_Settings = new Settings();
            }
        }
        Settings m_Settings = new Settings();
        DropBox m_DropBox;
        DropBox dropBox
        {
            get
            {
                if (m_DropBox == null)
                {
                    m_DropBox = new DropBox(textBoxEmail.Text, textBox2.Text);
                }
                return m_DropBox;
            }
        }


        private void listBoxJobs_DoubleClick(object sender, EventArgs e)
        {
            dropBox.Browse(listViewJobs.Items[listViewJobs.SelectedIndices[0]].Text);
        }

        private void buttonLocal_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            if (folderBrowserDialog1.SelectedPath != string.Empty)
            {
                textBoxLocalPath.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void listViewJobs_SelectedIndexChanged(object sender, EventArgs e)
        {
            buttonRemove.Enabled = true;
            buttonEdit.Enabled = true;
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            if (listViewJobs.SelectedIndices.Count == 0)
            {
                MessageBox.Show("Please select a job!");
                return;
            }
            buttonRemove.Enabled = false;
            buttonEdit.Enabled = false;
            SetDetails();
            
            m_Settings.jobs.RemoveAt(listViewJobs.SelectedIndices[0]);
            listViewJobs.Items.RemoveAt(listViewJobs.SelectedIndices[0]);
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            buttonRemove.Enabled = false;
            buttonEdit.Enabled = false;
            listViewJobs.Items.RemoveAt(listViewJobs.SelectedIndices[0]);
        }
        private void AddListViewItem(params string[] fields)
        {
            ListViewItem lineitem = new ListViewItem(fields[0]);
            lineitem.SubItems.Add(fields[1]);
            lineitem.SubItems.Add(fields[2]);
            lineitem.SubItems.Add(fields[3]);
            lineitem.SubItems.Add(fields[4]);
            listViewJobs.Items.Add(lineitem);
        }
        private void SetDetails()
        {
            if (listViewJobs.SelectedIndices.Count == 0)
            {
                MessageBox.Show("Please select a job!");
                return;
            }
            Job j = m_Settings.jobs[listViewJobs.SelectedIndices[0]];
            textBoxEmail.Text = j.email;
            textBoxKey.Text = j.key;
            textBoxLocalPath.Text = j.localPath;
            textBoxRemotePath.Text = j.remotePath;
        }
        private void ClearDetails()
        {
            textBoxEmail.Text = string.Empty;
            textBoxKey.Text = string.Empty;
            textBoxLocalPath.Text = string.Empty;
            textBoxRemotePath.Text = string.Empty;
        }
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string tokenPath = Application.StartupPath + "\\" + textBoxEmail.Text + ".AccessToken";
            dropBox.SaveToken(tokenPath);
            AddListViewItem(textBoxEmail.Text, tokenPath, textBoxLocalPath.Text, textBoxRemotePath.Text, textBoxKey.Text);
            m_Settings.jobs.Add(new Job() { email = textBoxEmail.Text, key = textBoxKey.Text, localPath = textBoxLocalPath.Text, name = "Job" + listViewJobs.Items.Count.ToString(), remotePath = textBoxRemotePath.Text, token = tokenPath });
            ClearDetails();
        }

        private void buttonKey_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            if (openFileDialog1.FileName != string.Empty)
            {
                textBoxKey.Text = openFileDialog1.FileName;
            }
        }

        private void buttonRemote_Click(object sender, EventArgs e)
        {
            SaveRemotePath del = delegate(string path) { textBoxRemotePath.Text = path; };
            SelectFolderDialog c = new SelectFolderDialog(dropBox, del);
            c.ShowDialog(this);
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            XMLSerializing.Serialize<Settings>(SETTINGS_PATH, m_Settings);
        }


    }
}
